package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.BrowsingHistory;
import com.atguigu.lease.web.app.mapper.BrowsingHistoryMapper;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.vo.history.HistoryItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author liubo
 * @description 针对表【browsing_history(浏览历史)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class BrowsingHistoryServiceImpl extends ServiceImpl<BrowsingHistoryMapper, BrowsingHistory>
        implements BrowsingHistoryService {
    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    @Override
    public IPage<HistoryItemVo> getPageByUserId(Page<HistoryItemVo> historyItemVoPage, Long userId) {
        return browsingHistoryMapper.getPageByUserId(historyItemVoPage, userId);
    }

    @Override
    /*异步调用*/
    @Async
    public void saveHistory(Long userId, Long roomId) {
        BrowsingHistory browsingHistory = super.getOne(new LambdaQueryWrapper<BrowsingHistory>()
                .eq(BrowsingHistory::getRoomId, roomId)
                .eq(BrowsingHistory::getUserId, userId));
        /*若是第一次访问此房间执行插入否则进行修改*/
        if (browsingHistory == null) {
            browsingHistory = new BrowsingHistory();
            browsingHistory.setBrowseTime(new Date());
            browsingHistory.setRoomId(roomId);
            browsingHistory.setUserId(userId);
            super.save(browsingHistory);
        } else {
            browsingHistory.setBrowseTime(new Date());
            super.updateById(browsingHistory);
        }
    }
}